package com.lzz.bottomencapsulation.app;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lzz.bottomencapsulation.eventbus.MessageEvent;
import com.lzz.bottomencapsulation.utils.LUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Fragment   第一层   基础封装
 * 实现懒加载(当前fragment显示时加载数据,且只加载一次)
 */

public abstract class BaseFragment extends Fragment {

    protected static final String TAG = "BaseFragment";

    /**
     * 当前页面视图
     */
    protected View view;
    /**
     * 页面是否启动
     */
    protected boolean isViewInitiated;
    /**
     * fragment 是否可见
     */
    protected boolean isVisibleToUser;
    /**
     * 数据是否已经加载
     */
    protected boolean isDataInitiated;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LUtils.v(TAG, "onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LUtils.v(TAG, "onCreate");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LUtils.v(TAG, "onActivityCreated");
        isViewInitiated = true;
        prepareFetchData();
     }

    /**
     * at "onCreateView "  Method   after
     * 在"onCreateView"方法之后调用
     * 初始化页面控件
     *
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LUtils.v(TAG, "onViewCreated");
    }

    /**
     * 设置视图
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        LUtils.v(TAG, "onCreateView");
        if (view == null) {
            view =inflater.inflate(getViewRes(), null);
        } else {
            /**
             * 缓存的rootView需要判断是否已经被加过parent，
             * 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
             */
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        if (view != null) initView();
        if (!EventBus.getDefault().isRegistered(this)){ EventBus.getDefault().register(this); }//注册eventbus
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        LUtils.v(TAG, "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        LUtils.v(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        LUtils.v(TAG, "onPause");
    }


    @Override
    public void onStop() {
        super.onStop();
        LUtils.v(TAG, "onStop");
    }

    /**
     * 取消注册
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        view=null;
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

        LUtils.v(TAG, "onDestroyView");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LUtils.v(TAG, "onDetach");
    }

    /**
     * //setUserVisibleHint()在Fragment创建时会先被调用一次，传入isVisibleToUser = false
     * //如果当前Fragment可见，那么setUserVisibleHint()会再次被调用一次，传入isVisibleToUser = true
     * //如果Fragment从可见->不可见，那么setUserVisibleHint()也会被调用，传入isVisibleToUser = false
     * //总结：setUserVisibleHint()除了Fragment的可见状态发生变化时会被回调外，在new Fragment()时也会被回调
     * //如果我们需要在 Fragment 可见与不可见时干点事，用这个的话就会有多余的回调了，那么就需要重新封装一个
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LUtils.v(TAG, "setUserVisibleHint" + isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    public boolean prepareFetchData() {
        return prepareFetchData(false);
    }

    public boolean prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            getSourceData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }

    /**
     * fragment  設置视图
     * @return
     */
    public abstract int getViewRes();

    /**
     *  初始化视图显示
     */
    public abstract void initView();

    /**
     * / 获取数据
     */
    public abstract void getSourceData();


    /**
     * eventbus 接收
     * @param messageEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {

    }
}
